home *** CD-ROM | disk | FTP | other *** search
- /* A Simple MPW C Terminal Emulator
- *
- * New File: May 20,1986
- * Jerry LeVan
- * 325 Boone Trail Richmond Ky 40475
- */
-
- # include <types.h> /* Nearly always required */
- # include <quickdraw.h> /* To access the qd globals */
- # include <fonts.h> /* Only for InitFonts() trap */
- # include <windows.h>
- # include <events.h>
- # include <menus.h>
- # include <segload.h> /* UnloadSeg() */
- # include <textedit.h> /* TEInit */
- # include <dialogs.h> /* InitDialogs */
- # include <desk.h> /* Open desk accessory */
- # include <toolutils.h>
- # include <serial.h>
- # include <errors.h>
- # include <OSUtils.h>
-
- Rect dragRect; /* limit rect for dragging rectangles on the screen */
- Rect growRect; /* limit rect for growing rectangles */
-
- WindowPtr myWindow; /* the application window */
-
- MenuHandle DeskMenu;
- MenuHandle FileMenu;
- MenuHandle EditMenu;
- MenuHandle OptionMenu;
- MenuHandle BaudMenu;
- MenuHandle ParityMenu;
- MenuHandle DataSizeMenu;
- MenuHandle PhoneMenu;
- MenuHandle FlowMenu;
- MenuHandle BSMenu;
- MenuHandle LocalMenu;
- MenuHandle CreatorMenu;
- MenuHandle XPrefsMenu;
- MenuHandle PortMenu;
-
- #define Desk_ID 128 /* Id for the apple menu */
- #define File_ID 129 /* Id for the file menu */
- #define Edit_ID 130 /* Id for the standard edit menu */
- #define Option_ID 131 /* Id for option list */
- #define Baud_ID 132 /* id for heirarchieal? menu */
- #define Parity_ID 133 /* id for parity menu */
- #define DataSize_ID 134 /* id for data size menu */
- #define Phone_ID 135 /* id for phone menu */
- #define Flow_ID 136 /* id for phone control menu */
- #define BS_ID 137 /* id for <bs> processing */
- #define Local_ID 138 /* id for full/half duplex flag */
- #define Creator_ID 139 /* id for text file creator menu */
- #define XPrefs_ID 140 /* id for xmodem preferences */
- #define Port_ID 141 /* id for port selection menu */
-
- #define windowID 128 /* id for application window */
- #define aboutID 128 /* id for about dialog */
-
- #define AIn -6
- #define AOut -7
- #define BIn -8
- #define BOut -9
-
- #define aboutItem 1 /* Item number for the about dialog */
-
- extern _DataInit(); /* The Rumtime Initialization routine */
-
- #define POPUP 0x0B /* trap number for popupmenuselect */
- #define UNIMPLEMENTED 0x9F /* "unimplemented" trap number */
-
- #define CR 0x0d
-
- /* this is defined in version two of MPW C */
- pascal long PopUpMenuSelect(menuh,top,left,item)
- MenuHandle menuh;
- short top;
- short left;
- short item;
- extern 0xa80b;
-
- Boolean moreToDo = true; /* set to false to exit program */
-
- /* Globals for this application */
-
- /*standard modem configuration */
- short config = baud1200 + stop10 + noParity + data8;
- short ourPort = sPortA; /* Standard modem port for now */
- short sIn = AIn;
- short sOut = AOut;
- short baud = baud1200; /* 1200 baud */
- short stopBits = stop10; /* one stop bit */
- short parity = noParity; /* no parity */
- short data = data8; /* eight data bits */
-
- short localEcho = 0; /* full duplex */
- short BSflag = 0; /* send BS as Delete */
- char XonXoff = 1; /* flow control enabled */
- char doCrLf = 0; /* true => echo cr as crlf */
- Boolean interpret_control = true; /* false means display on screen */
-
- /* defined in File.c (for text file up/down load) */
- extern Boolean OkToSend();
- extern Boolean OkToReceive();
-
-
- /* the following five items track the users xmodem preferences */
- /* and are defined in the file xmodem.c */
- extern char MacBinary ; /* true means try macbinary else standard xmodem */
- extern char TextXMode ; /* true means append Lf after Cr on upload */
- /* and strip Lf after Cr on download */
-
- extern char fastDL; /* true implies a fast ack */
- extern char forceCheckSum; /* true means disable CRC */
-
- extern long sectorSize; /* size of tranfer buffer */
-
- /******************************************************************/
- long creator = 'MPS '; /* default Text file creator signature */
-
- Boolean asciiReceive = false; /* true means we are receiving a text file */
- short recRef; /* reference number for received text file */
-
- Boolean asciiSend = false; /* true means we are sending a text file */
- short sendRef; /* reference number for file we are sending */
-
- short vRef; /* volume reference number for flush */
-
- /* the following variable is defined in clock.c */
- extern Boolean showClock ; /* if true show time in menu bar */
-
- /**********************************************************************/
-
- /* the following three variables control line pacing on text uploads */
- Boolean doPacing = false; /* true means line pacing on upload */
- char paceCh = ':'; /* if pacing wait for this character */
- long delayAmt = 0; /* delay time between characters(ticks) */
- Boolean foundPaceChar; /* true means we are in a paced line upload */
- /* the following routine is defined in Vt100.c */
- extern Boolean OKToSendChar(); /* test to see if ok to send char */
-
- /**********************************************************************/
- void SetLimitRects()
- { SetRect(&dragRect,qd.screenBits.bounds.left+4,
- qd.screenBits.bounds.top+24,
- qd.screenBits.bounds.right-4,
- qd.screenBits.bounds.bottom-4);
-
- SetRect(&growRect,qd.screenBits.bounds.left,
- qd.screenBits.bounds.top+24,
- qd.screenBits.bounds.right,
- qd.screenBits.bounds.bottom);
- }
- /**********************************************************************/
- void DoAboutDialog()
- { GrafPtr savePort;
- DialogPtr theDialog;
- short itemType;
- Handle itemHdl;
- Rect itemRect;
- short itemHit;
- EventRecord event;
-
- GetPort(&savePort);
- HideWindow(myWindow);
-
- theDialog = GetNewDialog(aboutID ,0,(WindowPtr)-1); /* create the dialogue */
- SetPort(theDialog);
-
- DrawDialog(theDialog);
-
- while(!GetNextEvent(mDownMask+keyDownMask+autoKeyMask,&event)) ;
- DisposDialog(theDialog);
-
- ShowWindow(myWindow);
- SetPort(savePort);
-
- }
-
-
- /**********************************************************************/
- void DoUpDate()
- { /* refresh the window */
- term_redraw();
- }
- /**********************************************************************/
- void DoMenu(menuresult)
- long menuresult;
- {
- short menuID, itemNumber,i;
- char AccessoryName[80];
-
- menuID = HiWord(menuresult);
- itemNumber = menuresult;
- stop_cursor();
- switch ( menuID )
- {
- case File_ID:
- switch(itemNumber){
- case 1: if(asciiReceive){
- FSClose(recRef); recRef = 0;
- asciiReceive = false;
- SetItem(FileMenu,itemNumber,"Start Text Capture...");
- FlushVol(vRef);
- }
- else
- if (OkToReceiveFile(&recRef,&vRef,creator)){
- SetItem(FileMenu,itemNumber,"Stop Text Capture");
- asciiReceive = true;
- }
- break;
- case 2: if(asciiSend){
- FSClose(sendRef);sendRef = 0;
- asciiSend = false;
- SetItem(FileMenu,itemNumber,"Start Text Upload...");
- }
- else
- if(OkToSendFile(&sendRef)){
- SetItem(FileMenu,itemNumber,"Stop Text Upload");
- asciiSend = true;
- foundPaceChar = false;}
- break;
- case 4: HideWindow(myWindow);
- DoPaceDialog(&paceCh,&doPacing,&delayAmt);
- UnloadSeg(DoPaceDialog);
- ShowWindow(myWindow);
- break;
- case 6: FlowControl(sOut,0); /* turn flow control off */
- Xreceive(); UnloadSeg(Xreceive);
- if(XonXoff)FlowControl(sOut,1); /* reset if necessary */
- break; /* xmodem receive file */
- case 7: FlowControl(sOut,0); /* turn off flow control */
- Xsend(); UnloadSeg(Xsend);
- if(XonXoff)FlowControl(sOut,1);
- break; /* xmodem send a file */
- case 10: LaunchAppl(ourPort,asciiReceive,recRef,asciiSend,sendRef);
- UnloadSeg(LaunchAppl);
- break;
- case 12: /* shutdown stuff */
- RAMSDClose(ourPort);
- if(showClock)stopClock(); /* remove timer queue element */
- moreToDo = false;
- break;
- default: break;
- }
- break;
- case Desk_ID:
- if(itemNumber == aboutItem) { DoAboutDialog(); break;}
- GetItem(DeskMenu, itemNumber, AccessoryName);
- OpenDeskAcc(AccessoryName);
- break;
- case Edit_ID:
- SystemEdit(itemNumber-1);
- break;
- case Baud_ID:
- for(i=1;i<=7;i++)CheckItem(BaudMenu,i,0);
- switch (itemNumber)
- { case 1: baud = baud300; break;
- case 2: baud = baud1200; break;
- case 3: baud = baud2400; break;
- case 4: baud = baud4800; break;
- case 5: baud = baud9600; break;
- case 6: baud = baud19200; break;
- case 7: baud = baud57600; break;
- }
- CheckItem(BaudMenu,itemNumber,1);
- SerReset(sOut,baud + stopBits + data + parity);
- SerReset(sIn, baud + stopBits + data + parity);
- break;
- case Local_ID:
- switch(itemNumber){
- case 1: localEcho = 1 ;
- CheckItem(LocalMenu,1,1);
- CheckItem(LocalMenu,2,0);
- break;
- case 2: localEcho = 0 ;
- CheckItem(LocalMenu,1,0);
- CheckItem(LocalMenu,2,1);
- break;
- default: break;
- }
- break;
- case BS_ID:
- switch(itemNumber){
- case 1: BSflag = 0 ;
- CheckItem(BSMenu,1,1);
- CheckItem(BSMenu,2,0);
- break;
- case 2: BSflag = 1 ;
- CheckItem(BSMenu,1,0);
- CheckItem(BSMenu,2,1);
- break;
- default: break;
- }
- break;
- case Flow_ID:
- switch(itemNumber){
- case 1: XonXoff = 1 ;
- CheckItem(FlowMenu,1,1);
- CheckItem(FlowMenu,2,0);
- break;
- case 2: XonXoff = 0 ;
- CheckItem(FlowMenu,1,0);
- CheckItem(FlowMenu,2,1);
- break;
- default: break;
- }
- FlowControl(sOut,XonXoff);
- break;
- case Option_ID:
- switch(itemNumber){
- case 9: showClock = !showClock;
- CheckItem(OptionMenu,9,showClock);
- if(showClock)startClock();
- else stopClock();
- break;
- case 10: Control(sOut,22,nil); break;
- case 11: doCrLf = !doCrLf;
- CheckItem(OptionMenu,11,doCrLf);
- break;
- case 12: ShowReadStats(); break;
- case 14: interpret_control = !interpret_control;
- CheckItem(OptionMenu,14,interpret_control);
- break;
- case 15: reset_term();
- break;
- case 16:
- HideWindow(myWindow);
- SelectCursorStyle();
- UnloadSeg(SelectCursorStyle);
- ShowWindow(myWindow);
- break;
- default: break;
- }
- break;
- case Phone_ID:
- HideWindow(myWindow);
- PhoneManager(sOut);UnloadSeg(PhoneManager);
- ShowWindow(myWindow);
- break;
- case Creator_ID: /* order must match menu */
- for(i=1; i<=4 ; i++) CheckItem(CreatorMenu,i,0);
- switch(itemNumber){
- case 1: creator = 'EDIT'; break;
- case 2: creator = 'MACA'; break;
- case 3: creator = 'MSWD'; break;
- case 4: creator = 'MPS '; break;
- default: break;
- }
- CheckItem(CreatorMenu,itemNumber,1);
- break;
- case XPrefs_ID:
- switch(itemNumber){
- case 1: /* MacBinary */
- CheckItem(XPrefsMenu,1,1);
- CheckItem(XPrefsMenu,2,0);
- CheckItem(XPrefsMenu,3,0);
- MacBinary = 1;
- TextXMode = 0;
- break;
- case 2: /* Text */
- CheckItem(XPrefsMenu,1,0);
- CheckItem(XPrefsMenu,2,1);
- CheckItem(XPrefsMenu,3,0);
- MacBinary =0;
- TextXMode =1;
- break;
- case 3: /* straight */
- CheckItem(XPrefsMenu,1,0);
- CheckItem(XPrefsMenu,2,0);
- CheckItem(XPrefsMenu,3,1);
- MacBinary = 0;
- TextXMode = 0;
- break;
- case 5: forceCheckSum = !forceCheckSum;
- CheckItem(XPrefsMenu,5,forceCheckSum);
- break;
- case 6: if(sectorSize == 128){
- sectorSize = 1024;
- SetItem(XPrefsMenu,6,"1024 Byte Buffer");}
- else{
- sectorSize = 128;
- SetItem(XPrefsMenu,6,"128 Byte Buffer");}
- CheckItem(XPrefsMenu,6,1);
- break;
- case 7: fastDL = !fastDL;
- CheckItem(XPrefsMenu,7,fastDL);
- break;
- default: break;
- }
- break;
- default: break;
- } /* switch menuID */
- start_cursor();
- HiliteMenu(0);
- }
- /**********************************************************************/
- # define __SEG__ Initialize
-
- Boolean InitProgram()
- { /* Standard Initialization Sequence */
-
- long menuresult;
- short itemNumber,menuID;
-
- /* Standard Manager Initialization Sequence */
- MoreMasters(); MoreMasters();
- InitGraf(&qd.thePort); /* set up quickdraw */
- InitFonts(); /* Set up fonts */
- FlushEvents(everyEvent,0); /* empty the event queue */
- InitWindows(); /* Set up window manager */
- InitMenus(); /* Set up menus */
- TEInit(); /* Needed for Dialogs */
- InitDialogs(nil); /* No restart proc */
-
- /* check to make sure we have popupmenus etc */
- if(NGetTrapAddress(POPUP,ToolTrap) ==
- NGetTrapAddress(UNIMPLEMENTED,ToolTrap)) {
- ErrorMessage("System 4.1 or later is required",0);
- return false;
- }
-
- /* Now the program stuff */
-
- /* Desk Accessory menu */
- DeskMenu = GetMenu(Desk_ID);
- AddResMenu(DeskMenu, 'DRVR');
- InsertMenu(DeskMenu, 0);
-
- /* "File" menu */
- FileMenu = GetMenu(File_ID);
- InsertMenu(FileMenu,0);
-
- /* "Edit" menu */
- EditMenu = GetMenu(Edit_ID);
- InsertMenu(EditMenu,0);
-
- /* "Baud" Menu */
- BaudMenu = GetMenu(Baud_ID);
- InsertMenu(BaudMenu,-1); /* Heirarchial menu insert */
-
- /* "Parity" Menu */
- ParityMenu = GetMenu(Parity_ID);
- InsertMenu(ParityMenu,-1);
-
- /* "DataSize" Menu */
- DataSizeMenu = GetMenu(DataSize_ID);
- InsertMenu(DataSizeMenu,-1);
-
- /* "Option" Menu */
- OptionMenu = GetMenu(Option_ID);
- InsertMenu(OptionMenu,0);
-
- /* Phone Menu */
- PhoneMenu = GetMenu(Phone_ID);
- InsertMenu(PhoneMenu, 0);
-
- /* flow control menu */
- FlowMenu = GetMenu(Flow_ID);
- InsertMenu(FlowMenu,-1);
-
- /* BS menu */
- BSMenu = GetMenu(BS_ID);
- InsertMenu(BSMenu,-1);
-
- /* Local echo menu */
- LocalMenu = GetMenu(Local_ID);
- InsertMenu(LocalMenu,-1);
-
- /* Text file creator menu */
- CreatorMenu = GetMenu(Creator_ID);
- InsertMenu(CreatorMenu,-1);
-
- /* Xmodem preferences menu */
- XPrefsMenu = GetMenu(XPrefs_ID);
- InsertMenu(XPrefsMenu,-1);
-
- /* Port Selection menu */
- PortMenu = GetMenu(Port_ID);
- InsertMenu(PortMenu,-1);
-
- DrawMenuBar();
-
- /* create the application window */
- myWindow = GetNewWindow(windowID, 0, (WindowPtr) -1);
- SetPort(myWindow);
- SetLimitRects();
-
- /* if mouse is down select port */
- menuresult=PopUpMenuSelect(PortMenu,100,200,1);
- menuID = HiWord(menuresult);
- itemNumber = menuresult;
- if(menuID == Port_ID)
- switch(itemNumber){
- case 1: ourPort = sPortA;
- sIn = AIn;
- sOut = AOut;
- break;
- case 2: ourPort = sPortB;
- sIn = BIn;
- sOut = BOut;
- break;
- default: break;
- }
- InitModem(ourPort,config); /* set up the modem */
- InitPage(); /* set up screen handler */
- setup_crc_tables(); /* set up crc table */
- InitClock(); /* turn on the clock */
- InitCursor(); /* last initialization */
- return true;
- }
-
-
- /**********************************************************************/
- # define __SEG__ Main
- MainEventLoop()
- { GrafPtr tmpWindow;
- WindowPtr whichwindow;
- EventRecord event;
- long sel,cnt,tmp;
- unsigned char ch,code;
- short err;
-
- SystemTask(); /* allow desk accessories time */
-
- /* Check if we are uploading a file */
- if(asciiSend)
- if (OKToSendChar(paceCh,doPacing,sOut,&foundPaceChar))
- {
- cnt = 1;
- err = FSRead(sendRef,&cnt,&ch);
- if(!err) {
- ProcessChar(ch,0,sOut,0); /* dummy code */
- Delay(delayAmt,&tmp);
- if(ch == CR) foundPaceChar = false;
- }
- else {if (err != eofErr)
- ErrorMessage("Error During File Read",err);
- FSClose(sendRef); sendRef = 0;
- SetItem(FileMenu,2,"Start Text Upload...");
- asciiSend = false; }
- }
-
-
- if (GetNextEvent(everyEvent,&event))
- switch (event.what) {
- case mouseDown:
- switch (FindWindow(&event.where,&whichwindow)) {
- case inMenuBar:
- if (sel = MenuSelect(&event.where))
- DoMenu(sel);
- break;
- case inSysWindow:
- SystemClick(&event,whichwindow);
- break;
- case inDrag:
- DragWindow(whichwindow,&event.where,&dragRect);
- break;
- case inGoAway:
- if (TrackGoAway(whichwindow,&event.where))
- /* action exit(); */
- break;
- case inGrow:
- case inContent:
- if (whichwindow != FrontWindow())
- SelectWindow(whichwindow);
- break;
- }
- break;
- case keyDown:
- case autoKey:
- if (myWindow == FrontWindow()) {
- ch = (char)event.message;
- code = (event.message & keyCodeMask) >> 8;
- if (event.modifiers & cmdKey) {
- if(!HiWord(MenuKey(ch))) /* not a system item */
- ch = ch & 0x1F ; /* convert to control key */
- }
- ProcessChar(ch,code,sOut,event.modifiers) ; /* write char to modem */
- }
- break;
- case app2Evt: if (showClock) showTime();
- break;
- case activateEvt:
- /* in this simple shell we will turn off/on the edit menu */
- whichwindow = (WindowPtr) event.message;
- if(whichwindow == myWindow){ /* It's our window */
- if(event.modifiers & activeFlag) /* and it's going active */
- {
- SetPort(whichwindow); /* make SURE drawing works */
- DisableItem(EditMenu,0);/* disable entire menu */
- DrawMenuBar(); /* and redraw the menu bar */
- start_cursor(); /* crank up the cursor */
- }
- else /* our window has been deactivated */
- { EnableItem(EditMenu,0); /* Turn on standard edit items */
- DrawMenuBar();
- stop_cursor(); /* stop the cursor */
- }
- }
-
- break;
- case updateEvt:
- whichwindow = (WindowPtr) event.message;
- if (whichwindow == myWindow){ /* it's our window */
- GetPort(&tmpWindow);SetPort(whichwindow); /* set port */
- BeginUpdate(whichwindow);
- DoUpDate();
- EndUpdate(whichwindow);
- SetPort(tmpWindow); /* restore previous port */
- }
- break;
- }
- }
- main(){
-
- UnloadSeg(_DataInit); /* unload the runtime initializer code */
-
- if(!InitProgram()) return; /* Initialization fails -> quit */
-
- UnloadSeg(InitProgram); /* Unload initialization code */
-
- while(moreToDo){
- docursor(); /* flash cursor */
- ReadPort(sIn); /* echo any characters from remote system */
- MainEventLoop();
- }
- }